687C - The Values You Can Make - CodeForces Solution


dp *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#include <random>
#include<algorithm>
using namespace std;
using int64 = long long;
using uint = unsigned int;

void solve(){
    int n, k;
    cin>>n>>k;
    vector<int> a(n);
    for(int i = 0; i < n; ++i) cin>>a[i];
    vector<vector<int>> f(k + 1, vector<int>(k + 1, 0));
    f[0][0] = 1; 
    for(int i = 0; i < n; ++i){
        vector<vector<int>> g = f;
        for(int j = 0; j <= k; ++j){
            for(int x = 0; x <= k; ++x){
                if(j + a[i] <= k) g[j + a[i]][x] += f[j][x];
                if(x + a[i] <= k) g[j][x + a[i]] += f[j][x];
            }
        }
        f = g;
    }
    vector<int> res;
    for(int i = 0; i <= k; ++i){
        if(f[i][k - i] || f[k - i][i]> 0) res.push_back(i);
    }
    cout<<res.size()<<'\n';
    for(auto c : res) cout<<c<<' ';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = 1;
    // int t;
    // cin>>t;
    
    while(t--){
        solve();
    }
    return 0;
} 


Comments

Submit
0 Comments
More Questions

2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves